<template>
  <Modal v-model:visible="showModal" title="辖区人口情况" width="60vw">
    <div class="flex flex-col">
      <div class="flex flex-row items-center mb-1">
        <a-select
          v-model:value="searchParams.year"
          :options="utils.genSelectOptions('year')"
          @change="loadData"
        ></a-select>
      </div>
      <vxe-table
        :loading="true"
        v-bind="customTableStyle"
        :data="tableData"
        class="custom-vxe-table"
      >
        <vxe-column field="year" title="年份" width="80">
          <template #default="{ row }"> {{ row.year }}年 </template>
        </vxe-column>
        <vxe-column field="week" title="周次" minWidth="80">
          <template #default="{ row }"> {{ row.week }}周 </template>
        </vxe-column>
        <vxe-column field="totalCount" title="实有人口" width="150" :sortable="true">
          <template #default="{ row }"> {{ row.totalCount }}人 </template>
        </vxe-column>
      </vxe-table>
    </div>
  </Modal>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import Modal from '@/components/modal.vue'
import { Apis } from '@/api'
import { message } from 'ant-design-vue'
import * as utils from '../../utils'

const isLoading = ref(false)

const showModal = ref(false)

const searchParams = ref({
  year: '',
  week: '',
})
const tableData = ref<any[]>([])

// 统一管理表格样式
const customTableStyle = {
  ...utils.customTableStyle,
  footerMethod: ({ columns, data }) => {
    const footerData = [
      columns.map((column, columnIndex) => {
        if (columnIndex === 0) {
          return '合计'
        }
        if (['totalCount', 'hjCount', 'flowInCount', 'flowOutCount'].includes(column.field)) {
          return (utils.sumNum(data, column.field) || '') + '人'
        }

        return ''
      }),
    ]
    return footerData
  },
}

function loadData() {
  isLoading.value = true
  Apis.szbt.populationData
    .get_getChartData({
      params: {
        year: searchParams.value.year,
        month: 0,
        week: searchParams.value.week,
      },
    })
    .then((res) => {
      tableData.value = (res.barData || [])
        .sort((a: any, b: any) => {
          // 先按年份倒序
          if (b.year !== a.year) {
            return b.year - a.year
          }
          // 再按月份倒序
          if (b.week !== a.week) {
            return b.week - a.week
          }
          return b.totalCount - a.totalCount
        })
        .map((item: any, index: number) => {
          item.index = index + 1
          const totalCount = item.totalCount || 0
          item.totalCount = totalCount
          item.flowInPercentage = totalCount
            ? (((item.flowInCount || 0) / totalCount) * 100).toFixed(2)
            : 0 // 计算流入占总人口的比例
          item.flowOutPercentage = totalCount
            ? (((item.flowOutCount || 0) / totalCount) * 100).toFixed(2)
            : 0 // 计算流出占总人口的比例
          return item
        })
    })
    .catch((err) => {
      message.error('获取数据失败')
    })
    .finally(() => {
      isLoading.value = false
    })
}

defineExpose({
  setData: (data: any) => {
    const { params } = data
    searchParams.value = params
    // tableData.value = data;
  },
  open: () => {
    showModal.value = true
    loadData()
  },
})
</script>

<style lang="scss" src="@/styles/custom-vxe-table.scss"></style>
